package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.adapter.ListAdapter;
import com.shoujifeng.snowmusic.adapter.adapt;
import com.shoujifeng.snowmusic.entity.Mp3Info;
import com.shoujifeng.snowmusic.player.http.GlobalValue;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.LoadMask;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;
import com.shoujifeng.snowmusic.player.utils.TabHostAnim;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 在线音乐榜单
 * 
 * @author Administrator
 * 
 */
public class OnlineOrderActivity extends Activity {
	private adapt mAdapter;
	private Thread accThread;
	private ListView listView;
	private ServerAccess serverAccess;
	private final int UPDATE_ONE = 0;
	private final int UPDATE_TWO = 1;
	private final int UPDATE_THREE = 2;
	private final int UPDATE_FOUR = 3;
	private final int UPDATE_FIVE = 4;
	private ArrayList<HashMap<String, Object>> mOrderListOne;
	private ArrayList<HashMap<String, Object>> mOrderListTwo;
	private ArrayList<HashMap<String, Object>> mOrderListThree;
	private ArrayList<HashMap<String, Object>> mOrderListFour;
	
	private RelativeLayout lay01;
	private RelativeLayout lay02;
	private RelativeLayout lay03;
	private RelativeLayout lay04;
	private RelativeLayout lay05;
	
	//娴佽姝屾洸
	private TextView oneNameLay1;
	private TextView twoNameLay1;
	private TextView threeNameLay1;
	//缁忓吀姝屾洸
	private TextView oneNameLay2;
	private TextView twoNameLay2;
	private TextView threeNameLay2;
	//钘忔棌寮瑰敱
	private TextView oneNameLay3;
	private TextView twoNameLay3;
	private TextView threeNameLay3;
	//钘忔棌鑸炴洸
	private TextView oneNameLay4;
	private TextView twoNameLay4;
	private TextView threeNameLay4;
	
	private LoadMask mLoadMask;
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.online_order_lay);
		init();
		initGlobalValue();
		mLoadMask = new LoadMask(this);
		mLoadMask.startLoad();
		accThread = new Thread(runnable);
		accThread.start();
	}

	private void init() {
		serverAccess = new ServerAccess(OnlineOrderActivity.this);
		lay01 = (RelativeLayout) findViewById(R.id.lay01);
		lay02 = (RelativeLayout) findViewById(R.id.lay02);
		lay03 = (RelativeLayout) findViewById(R.id.lay03);
		lay04 = (RelativeLayout) findViewById(R.id.lay04);
		lay01.setOnClickListener(onClickListener);
		lay02.setOnClickListener(onClickListener);
		lay03.setOnClickListener(onClickListener);
		lay04.setOnClickListener(onClickListener);
		
		oneNameLay1 = (TextView) findViewById(R.id.oneNameLay1);
		twoNameLay1 = (TextView) findViewById(R.id.twoNameLay1);
		threeNameLay1 = (TextView) findViewById(R.id.threeName3Lay1);
		
		oneNameLay2 = (TextView) findViewById(R.id.oneNameLay2);
		twoNameLay2 = (TextView) findViewById(R.id.twoNameLay2);
		threeNameLay2 = (TextView) findViewById(R.id.threeNameLay2);
		
		oneNameLay3 = (TextView) findViewById(R.id.oneNameLay3);
		twoNameLay3 = (TextView) findViewById(R.id.twoNameLay3);
		threeNameLay3 = (TextView) findViewById(R.id.threeNameLay3);
		
		oneNameLay4 = (TextView) findViewById(R.id.oneNameLay4);
		twoNameLay4 = (TextView) findViewById(R.id.twoNameLay4);
		threeNameLay4 = (TextView) findViewById(R.id.threeNameLay4);
		
	}


	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.lay01:
			{
				Intent  intent = new Intent(OnlineOrderActivity.this,OnlineOrderContentActivity.class);
				intent.putExtra("titleName", "流行歌曲");
				intent.putExtra(ServerAccess.INDEX, "0");
				startActivity(intent);
			}
				break;
			case R.id.lay02:
			{
				Intent  intent = new Intent(OnlineOrderActivity.this,OnlineOrderContentActivity.class);
				intent.putExtra("titleName", "经典歌曲");
				intent.putExtra(ServerAccess.INDEX, "1");
				startActivity(intent);
			}
				break;
			case R.id.lay03:
			{
				Intent  intent = new Intent(OnlineOrderActivity.this,OnlineOrderContentActivity.class);
				intent.putExtra("titleName", "藏族弹唱");
				intent.putExtra(ServerAccess.INDEX, "2");
				startActivity(intent);
			}
				break;
			case R.id.lay04:
			{
				Intent  intent = new Intent(OnlineOrderActivity.this,OnlineOrderContentActivity.class);
				intent.putExtra("titleName", "藏族舞曲");
				intent.putExtra(ServerAccess.INDEX, "3");
				startActivity(intent);
			}
				break;


			default:
				break;
			}
			
			TabHostAnim.set(R.anim.right_in, R.anim.no_move);
		}
	};
	protected Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			
			case UPDATE_ONE: {
				mLoadMask.stopLoad();
				
				if(mOrderListOne.size()==3){
					HashMap<String , Object> map = new HashMap<String, Object>();
					map = mOrderListOne.get(0);
					oneNameLay1.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListOne.get(1);
					twoNameLay1.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListOne.get(2);
					threeNameLay1.setText(map.get(ServerAccess.MUSIC_NAME).toString());
				}
				
			}
				break;
			case UPDATE_TWO: {
				if(mOrderListTwo.size()==3){
					HashMap<String , Object> map = new HashMap<String, Object>();
					map = mOrderListTwo.get(0);
					oneNameLay2.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListTwo.get(1);
					twoNameLay2.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListTwo.get(2);
					threeNameLay2.setText(map.get(ServerAccess.MUSIC_NAME).toString());
				}
			}
				break;	
				
			case UPDATE_THREE: {
				if(mOrderListThree.size()==3){
					HashMap<String , Object> map = new HashMap<String, Object>();
					map = mOrderListThree.get(0);
					oneNameLay3.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListThree.get(1);
					twoNameLay3.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListThree.get(2);
					threeNameLay3.setText(map.get(ServerAccess.MUSIC_NAME).toString());
				}
			}
				break;	
				
			case UPDATE_FOUR: {
				if(mOrderListFour.size()==3){
					HashMap<String , Object> map = new HashMap<String, Object>();
					map = mOrderListFour.get(0);
					oneNameLay4.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListFour.get(1);
					twoNameLay4.setText(map.get(ServerAccess.MUSIC_NAME).toString());
					map = mOrderListFour.get(2);
					threeNameLay4.setText(map.get(ServerAccess.MUSIC_NAME).toString());
				}
			}
				break;	
				

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	// 鍒涘缓澶氱嚎绋嬭幏鍙栨暟鎹?
	protected Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(OnlineOrderActivity.this);
				   String value = spf.getString(ServerAccess.SET_WIFI, "no");   //参数: 1-要读出的键值  2- 默认值(就是读取失败后返回的值)
				   boolean setWifi = ServerAccess.checkNetworkConnection(OnlineOrderActivity.this);
				
				if(value.equals("yes")&&setWifi){
					String[] keys = new String[] { ServerAccess.MUSIC_NAME };
					
					mOrderListOne = new ArrayList<HashMap<String, Object>>();
					mOrderListOne = serverAccess.getOrderListTwo(keys);
					sendMessage(UPDATE_ONE);
					
					mOrderListTwo = new ArrayList<HashMap<String, Object>>();
					mOrderListTwo = serverAccess.getOrderListThree(keys);
					sendMessage(UPDATE_TWO);
					
					mOrderListThree = new ArrayList<HashMap<String, Object>>();
					mOrderListThree = serverAccess.getOrderListFour(keys);
					sendMessage(UPDATE_THREE);
					
					mOrderListFour = new ArrayList<HashMap<String, Object>>();
					mOrderListFour = serverAccess.getOrderListOne(keys);
					sendMessage(UPDATE_FOUR);
				}
				if(value.equals("no")||setWifi){
					String[] keys = new String[] { ServerAccess.MUSIC_NAME };
					
					mOrderListOne = new ArrayList<HashMap<String, Object>>();
					mOrderListOne = serverAccess.getOrderListTwo(keys);
					sendMessage(UPDATE_ONE);
					
					mOrderListTwo = new ArrayList<HashMap<String, Object>>();
					mOrderListTwo = serverAccess.getOrderListThree(keys);
					sendMessage(UPDATE_TWO);
					
					mOrderListThree = new ArrayList<HashMap<String, Object>>();
					mOrderListThree = serverAccess.getOrderListFour(keys);
					sendMessage(UPDATE_THREE);
					
					mOrderListFour = new ArrayList<HashMap<String, Object>>();
					mOrderListFour = serverAccess.getOrderListOne(keys);
					sendMessage(UPDATE_FOUR);
				}

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	};

	// 鍙戦?娑堟伅鍒癏andler
	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}

	private void initGlobalValue() {
		GlobalValue.initSDAddr();

		GlobalValue.addUrl(ServerAccess.CHARTS_LIST,
				"http://www.snowmusic.com.cn/mobile_api/charts_list.php");
	}

}
